home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / getopt.c < prev    next >
C/C++ Source or Header  |  1990-09-06  |  3KB  |  103 lines

  1. /*
  2. From: gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>)
  3. Newsgroups: net.sources
  4. Subject: getopt library routine
  5. Date: 30 Mar 85 04:45:33 GMT
  6. */
  7. /*
  8.     getopt -- public domain version of standard System V routine
  9.  
  10.     Strictly enforces the System V Command Syntax Standard;
  11.     provided by D A Gwyn of BRL for generic ANSI C implementations
  12. */
  13.  
  14. #include     <stddef.h>
  15. #include    <stdio.h>
  16. #include    <string.h>
  17.  
  18. int    opterr = 1;            /* error => print message */
  19. int    optind = 1;            /* next argv[] index */
  20. char    *optarg = NULL;            /* option parameter if any */
  21.  
  22. static int
  23. Err( name, mess, c )            /* returns '?' */
  24.     char    *name;            /* program name argv[0] */
  25.     char    *mess;            /* specific message */
  26.     int    c;            /* defective option letter */
  27.     {
  28.     if ( opterr )
  29.         (void) fprintf( stderr,
  30.                 "%s: %s -- %c\n",
  31.                 name, mess, c
  32.                   );
  33.  
  34.     return '?';            /* erroneous-option marker */
  35.     }
  36.  
  37. int
  38. getopt( argc, argv, optstring )        /* returns letter, '?', EOF */
  39.     int        argc;        /* argument count from main */
  40.     const char    *argv[];    /* argument vector from main */
  41.     const char    *optstring;    /* allowed args, e.g. "ab:c" */
  42.     {
  43.     static int    sp = 1;        /* position within argument */
  44.     register int    osp;        /* saved `sp' for param test */
  45.     register int    c;        /* option letter */
  46.     register char    *cp;        /* -> option in `optstring' */
  47.  
  48.     optarg = NULL;
  49.  
  50.     if ( sp == 1 )            /* fresh argument */
  51.         if ( optind >= argc        /* no more arguments */
  52.           || argv[optind][0] != '-'    /* no more options */
  53.           || argv[optind][1] == '\0'    /* not option; stdin */
  54.            )
  55.             return EOF;
  56.         else if ( strcmp( argv[optind], "--" ) == 0 )
  57.             {
  58.             ++optind;    /* skip over "--" */
  59.             return EOF;    /* "--" marks end of options */
  60.             }
  61.  
  62.     c = argv[optind][sp];        /* option letter */
  63.     osp = sp++;            /* get ready for next letter */
  64.  
  65.     if ( argv[optind][sp] == '\0' )    /* end of argument */
  66.         {
  67.         ++optind;        /* get ready for next try */
  68.         sp = 1;            /* beginning of next argument */
  69.         }
  70.  
  71.     if ( c == ':'            /* optstring syntax conflict */
  72.       || (cp = strchr( optstring, c )) == NULL    /* not found */
  73.        )
  74.         return Err( argv[0], "illegal option", c );
  75.  
  76.     if ( cp[1] == ':' )        /* option takes parameter */
  77.         {
  78.         if ( osp != 1 )
  79.             return Err( argv[0],
  80.                     "option must not be clustered",
  81.                     c
  82.                   );
  83.  
  84.         if ( sp != 1 )        /* reset by end of argument */
  85.             return Err( argv[0],
  86.                    "option must be followed by white space",
  87.                     c
  88.                   );
  89.  
  90.         if ( optind >= argc )
  91.             return Err( argv[0],
  92.                     "option requires an argument",
  93.                     c
  94.                   );
  95.  
  96.         optarg = (char *)argv[optind];    /* make parameter available */
  97.         ++optind;        /* skip over parameter */
  98.         }
  99.  
  100.     return c;
  101.     }
  102.  
  103.